<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
// +----------------------------------------------------------------------+
// | Treacle PHP Framework Version 0.10                                   |
// +----------------------------------------------------------------------+
// | Copyright (c) 2007 Treacle.cn All rights reserved.					  |
// +----------------------------------------------------------------------+
// | Licensed under the Apache License, Version 2.0 (the 'License');      |
// | you may not use this file except in compliance with the License.     |
// | You may obtain a copy of the License at                              |
// | http://www.apache.org/licenses/LICENSE-2.0                           |
// | Unless required by applicable law or agreed to in writing, software  |
// | distributed under the License is distributed on an 'AS IS' BASIS,    |
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or      |
// | implied. See the License for the specific language governing         |
// | permissions and limitations under the License.                       |
// +----------------------------------------------------------------------+
// | Author: BoPo <ibopo@126.com>										  |
// +----------------------------------------------------------------------+
/**
 * 数据库查询结果类
 *
 * @author BoPo <ibopo@126.com>
 * @link http://www.treacle.cn/
 * @copyright Copyright &copy; 2007 Treacle
 * @license http://www.treacle.cn/license/
 * @version $Id $
 * @package database
 */
/**
 * 数据库查询结果类
 *
 * @package	database
 * @author BoPo <ibopo@126.com>
 * @since 0.10
 * @version	$Id $
 */
abstract class DB_result
{
	private $conn_id		= NULL;
	private $result_id		= NULL;
	private $result_array	= array();
	private $result_object	= array();
	private $current_row 	= 0;
	private $num_rows		= 0;

	function __get($var)
	{
		return (isset($this->$var))?$this->$var:null;
	}
	/**
	 * 查询结果.默认返回一个对象类型
	 *
	 * @access	public
	 * @param	string	参数 "object" 或 "array"
	 * @return	mixed	返回一个对象或者数组
	 */
	public function result($type = 'object')
	{
		return ($type == 'object') ? $this->result_object() : get_object_vars($this->result_object());
	}
	/**
	 * 以一个对象方式返回的查询结果
	 *
	 * @access	public
	 * @return	object
	 */
	public function result_object()
	{
		if (count($this->result_object) > 0)
		{
			return $this->result_object;
		}
		// In the event that query caching is on the result_id variable
		// will return FALSE since there isn't a valid SQL resource so
		// we'll simply return an empty array.
		if ($this->result_id === FALSE OR $this->num_rows() == 0)
		{
			return array();
		}
		$this->_data_seek(0);
		while ($row = $this->_fetch_object())
		{
			$this->result_object[] = $row;
		}
		return $this->result_object;
	}
	/**
	 * 以一个数组方式返回的查询结果
	 *
	 * @access	public
	 * @return	array
	 */
	public function result_array()
	{
		if (count($this->result_array) > 0)
		{
			return $this->result_array;
		}
		// In the event that query caching is on the result_id variable
		// will return FALSE since there isn't a valid SQL resource so
		// we'll simply return an empty array.
		if ($this->result_id === FALSE OR $this->num_rows() == 0)
		{
			return array();
		}
		$this->_data_seek(0);
		while ($row = $this->_fetch_assoc())
		{
			$this->result_array[] = $row;
		}
		return $this->result_array;
	}
	/**
	 * 返回一行查询结果
	 *
	 * @access	public
	 * @param	string
	 * @return	mixed
	 */
	public function row($n = 0, $type = 'object')
	{
		return ($type == 'object') ? $this->row_object($n) : $this->row_array($n);
	}
	/**
	 * 返回一行查询结果,对象方式
	 *
	 * @access	public
	 * @return	object
	 */
	public function row_object($n = 0)
	{
		$result = $this->result_object();
		if (count($result) == 0)
		{
			return $result;
		}
		if ($n != $this->current_row AND isset($result[$n]))
		{
			$this->current_row = $n;
		}
		return $result[$this->current_row];
	}
	/**
	 * 返回一行查询结果,数组方式
	 *
	 * @access	public
	 * @return	array
	 */
	public function row_array($n = 0)
	{
		$result = $this->result_array();
		if (count($result) == 0)
		{
			return $result;
		}
		if ($n != $this->current_row AND isset($result[$n]))
		{
			$this->current_row = $n;
		}
		return $result[$this->current_row];
	}
	/**
	 * 返回第一行记录
	 *
	 * @access	public
	 * @return	object
	 */
	public function first_row($type = 'object')
	{
		$result = $this->result($type);
		if (count($result) == 0)
		{
			return $result;
		}
		return $result[0];
	}
	/**
	 * 返回最后一行记录
	 *
	 * @access	public
	 * @return	object
	 */
	public function last_row($type = 'object')
	{
		$result = $this->result($type);
		if (count($result) == 0)
		{
			return $result;
		}
		return $result[count($result) -1];
	}
	/**
	 * 返回下一行记录
	 *
	 * @access	public
	 * @return	object
	 */
	public function next_row($type = 'object')
	{
		$result = $this->result($type);
		if (count($result) == 0)
		{
			return $result;
		}
		if (isset($result[$this->current_row + 1]))
		{
			++$this->current_row;
		}
		return $result[$this->current_row];
	}
	/**
	 * 返回上一行记录
	 *
	 * @access	public
	 * @return	object
	 */
	public function previous_row($type = 'object')
	{
		$result = $this->result($type);
		if (count($result) == 0)
		{
			return $result;
		}
		if (isset($result[$this->current_row - 1]))
		{
			--$this->current_row;
		}
		return $result[$this->current_row];
	}
	abstract public function num_rows();
	abstract public function num_fields();
	abstract public function list_fields();
	abstract public function field_names();
	abstract public function field_data() ;
	abstract public function free_result();
	abstract protected function _data_seek();
	abstract protected function _fetch_assoc() ;
	abstract protected function _fetch_object() ;
}
// +--------------------------------------------------------------------------
// |   Last Modified By BoPo
// |   Date: Sat Nov 10 14:19:36 CST 2007
// +--------------------------------------------------------------------------
?>